<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/1
 * Time: 15:02
 */
namespace App\Http\Controllers\Business\Shop\V1\OrderManagement;

use App\Models\Order;
use App\Models\ShopUser;

class OrderManagement {
    public static function orderList($shopid,$starttime,$lasttime){
        if(!$shopid || !$starttime || !$lasttime)
        {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $data['ordernum'] = Order::where('shopid',$shopid)->where('created_at','>=',$starttime.' 00:00:00')
            ->where('created_at','<=',$lasttime.' 23:59:59')->where('refundstatus', 0)->get()->count();
        if($data['ordernum'] == '') {
            $data['ordernum'] = '0';
        }
        $data['ordersum'] = Order::where('shopid',$shopid)->where('created_at','>=',$starttime.' 00:00:00')
            ->where('created_at','<=',$lasttime.' 23:59:59')->where('refundstatus', 0)->sum('shopamount');
        if($data['ordersum'] == '') {
            $data['ordersum'] = '0.0';
        }
        /*
        $data['orderlist'] = Order::where('orders.shopid',$shopid)
            ->where('orders.created_at','>=',$starttime.' 00:00:00')
            ->where('orders.created_at','<=',$lasttime.' 23:59:59')
            ->join("shop_users", 'shop_users.id', '=', 'orders.shopuserid')
            ->select('orders.ordernumber','orders.nickname','orders.shopamount','orders.created_at','orders.shopuserid', 'shop_users.name as shopusername')
            ->orderBy('orders.created_at','desc')->paginate(20);
        */
        $orderLists = Order::where('shopid', $shopid)->where('created_at','>=',$starttime.' 00:00:00')->where('created_at','<=',$lasttime.' 23:59:59')
            ->select('ordernumber','nickname','shopamount','created_at','shopuserid')->where('refundstatus', 0)
            ->orderBy('created_at', 'desc')->paginate(20);
        foreach($orderLists as $k=>$val) {
            $shopUserId = $orderLists[$k]['shopuserid'];
            if($shopUserId == 0 || $shopUserId=='') {
                $orderLists[$k]['shopusername'] = '默认';
            } else {
                $orderLists[$k]['shopusername'] = ShopUser::where('id', $shopUserId)->pluck('name')->first();
            }
        }
        $data['orderlist'] = $orderLists;
        return ['status'=>'success','data'=>$data];
    }

    public static function orderDetails($shopid,$ordernumber){
        if(!$shopid || !$ordernumber)
        {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        /*
        $data = Order::where('orders.shopid',$shopid)->where('orders.ordernumber',$ordernumber)
            ->join('users', 'orders.userid' , '=', 'users.id')
            ->join('shop_users', 'orders.shopuserid' , '=', 'shop_users.id')
            ->select('orders.ordernumber','orders.nickname','users.mobile','orders.shopamount','orders.created_at','orders.shopuserid', 'shop_users.name as shopusername','orders.status')->first();
        */
        $data = Order::where('orders.shopid',$shopid)->where('orders.ordernumber',$ordernumber)
            ->join('users', 'orders.userid' , '=', 'users.id')
            ->select('orders.ordernumber','orders.nickname','users.mobile','orders.shopamount','orders.created_at','orders.shopuserid', 'orders.accountstatus as status')->first();
        if(!$data) {
            return ['status'=>'failed','message'=>'获取信息失败'];
        }
        $shopUserId = $data['shopuserid'];
        if($shopUserId == 0 || $shopUserId == '') {
            $data['shopusername'] = '默认';
        } else {
            $data['shopusername'] = ShopUser::where('id', $shopUserId)->pluck('name')->first();
        }
        $data['mobile'] = str_replace(substr($data['mobile'],3,4),'****',$data['mobile']);
        return ['status'=>'success','data'=>$data];
    }
}